package com.biannuo.szk.demo.provider.controller;

import com.biannuo.szk.common.base.BaseResponse;
import com.biannuo.szk.common.bean.ComplexObject;
import com.biannuo.szk.common.bean.Params;
import com.biannuo.szk.common.bean.Result;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * 提供者控制器
 *
 * @author szk
 */
@RestController
public class DemoProviderController extends BaseResponse {
    /**
     * 提供给服务消费者调用的接口
     */
    @Value("${server.port}")
    private String serverPort;

    /**
     * 提供给服务消费者调用的接口
     *
     * @return
     */
    @RequestMapping("/openFeignProviderTest")
    public String openFeignProviderTest() {
        // 除数不能为0 ，此处必报错
//        int a = 1/0;
        return "DemoProviderController#openFeignProviderTest " + RandomUtils.nextInt(0, 1000);
    }

    /**
     * 提供给服务消费者调用的接口
     *
     * @param name
     * @param id
     * @return
     */
    @PostMapping("/sampleParamsProviderTest")
    public String sampleParamsProviderTest(@RequestParam("name") String name, @RequestParam("id") Integer id) {
        return "DemoProviderController#sampleParamsProviderTest " + serverPort + " id=" + id + " name=" + name;
    }

    /**
     * Fegin参数传递测试，简单对象
     *
     * @param params
     * @return
     */
    @GetMapping("/springQueryMapProviderTest")
    public String springQueryMapProviderTest(Params params) {
        return "DemoProviderController#springQueryMapProviderTest " + serverPort + " id=" + params.getId() + " name=" + params.getName();
    }

    /**
     * 提供给服务消费者调用的接口
     *
     * @param complexObject
     * @return
     */
    @PostMapping("/complexObjectProviderTest")
    public Result complexObjectProviderTest(@RequestBody ComplexObject complexObject) {
        Result result = new Result();
        result.setCode(200);
        result.setDescribe("DemoProviderController#complexObjectProviderTest " + serverPort + " id=" + complexObject.getParams().getId() + " name=" + complexObject.getParams().getName());
        return result;
    }

    @PostMapping("/complexObjectProviderTestException")
    public Result complexObjectProviderTest(@RequestBody ComplexObject complexObject, @RequestBody Params params) {
        Result result = new Result();
        result.setCode(200);
        result.setDescribe("DemoProviderController#complexObjectProviderTest " + serverPort + " id=" + complexObject.getParams().getId() + " name=" + complexObject.getParams().getName());
        return result;
    }
}